﻿@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.UserStore;
@using SimpleIdServer.IdServer.Website.Stores.AuthMethodsStore;
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IDispatcher dispatcher
@inject IState<UserState> userState
@inject IState<UpdateUserState> updateUserState
@inject IState<AuthMethodsState> authMethodsState
@inject NotificationService notificationService
@inject TooltipService tooltipService

<RadzenTemplateForm Submit=@UpdateUserSettings TItem="UpdateUserSettingsParameter" Data=@updateUserSettings>
    <!-- Identifier -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Identifier</RadzenText>
        <RadzenTextBox Name="Id" @bind-Value="@updateUserSettings.Id" Class="w-100" Disabled=true></RadzenTextBox>
    </div>
    <!-- Login -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Name</RadzenText>
        <RadzenTextBox Name="Login" @bind-Value="@updateUserSettings.Login" Class="w-100" Disabled=true></RadzenTextBox>
    </div>
    <!-- Created at -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.CreatedAt</RadzenText>
        <RadzenDatePicker @bind-Value=@updateUserSettings.CreateDateTime DateFormat="d" Class="w-100" Disabled=true />
    </div>
    <!-- Updated at -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.UpdatedAt</RadzenText>
        <RadzenDatePicker @bind-Value=@updateUserSettings.UpdateDateTime DateFormat="d" Class="w-100" Disabled=true />
    </div>
    <!-- Email -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Email</RadzenText>
        <RadzenTextBox Name="Email" @bind-Value="@updateUserSettings.Email" Class="w-100"></RadzenTextBox>
        <RadzenEmailValidator Component="Email" Text="Provide a valid email address"></RadzenEmailValidator>
    </div>
    <!-- First name -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Firstname</RadzenText>
        <RadzenTextBox Name="Firstname" @bind-Value="@updateUserSettings.Firstname" Class="w-100"></RadzenTextBox>
    </div>
    <!-- Last name -->
    <div>
        <RadzenText TextStyle="TextStyle.Subtitle2" TagName="TagName.H3">@Global.Lastname</RadzenText>
        <RadzenTextBox Name="Lastname" @bind-Value="@updateUserSettings.Lastname" Class="w-100"></RadzenTextBox>
    </div>
    <!-- Notification Mode -->
    <div>
        <div>
            <RadzenIcon Icon="info" MouseEnter="@(args => ShowTooltip(args, Global.NotificationModeTooltip))" />
            <RadzenLabel Text="@Global.NotificationMode" Style="padding: 0px;"></RadzenLabel>
        </div>
        <RadzenDropDown Class="w-100"
            Data=@authMethodsState.Value.AuthenticationMethods.Where(m => m.Capabilities.HasFlag(AuthenticationMethodCapabilities.PUSHNOTIFICATION))
            TextProperty="Name"
            ValueProperty="Id"
            TValue="string"
            @bind-Value=@updateUserSettings.NotificationMode></RadzenDropDown>
        
    </div>
    <RadzenButton class="mt-1" Variant="Variant.Flat" ButtonType="ButtonType.Submit" ButtonStyle="ButtonStyle.Success" Text="@(updateUserState.Value.IsUpdating ? Global.Updating : Global.Update)" Disabled=@updateUserState.Value.IsUpdating />
</RadzenTemplateForm>
@code {
    [Parameter]
    public SimpleIdServer.IdServer.Domains.User User { get; set; }

    UpdateUserSettingsParameter updateUserSettings = new UpdateUserSettingsParameter();

    class UpdateUserSettingsParameter
    {
        public string Id { get; set; } = null!;
        public string Login { get; set; } = null!;
        public DateTime CreateDateTime { get; set; }
        public DateTime UpdateDateTime { get; set; }
        public string? Email { get; set; } = null;
        public string? Firstname { get; set; } = null;
        public string? Lastname { get; set; } = null;
        public string? NotificationMode { get; set; } = null;
    }

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {            
            SubscribeToAction<UpdateUserDetailsSuccessAction>((act) =>
            {
                notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = Global.UserDetailsUpdated });
                StateHasChanged();
            });
        }
    }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();
        if (this.User == null) return;
        updateUserSettings = new UpdateUserSettingsParameter
        {
            Id = this.User.Id,
            Login = this.User.Name,
            CreateDateTime = this.User.CreateDateTime,
            UpdateDateTime = this.User.UpdateDateTime,
            Email = this.User.Email,
            Firstname = this.User.Firstname,
            Lastname = this.User.Lastname,
            NotificationMode = this.User.NotificationMode
        };
    }

    void UpdateUserSettings(UpdateUserSettingsParameter parameter)
    {
        var act = new UpdateUserDetailsAction { Email = parameter.Email, Firstname = parameter.Firstname, Lastname = parameter.Lastname, UserId = this.User.Id, NotificationMode = parameter.NotificationMode };
        dispatcher.Dispatch(act);
    }

    void ShowTooltip(ElementReference eltRef, string msg)
    {
        tooltipService.Open(eltRef, msg, null);
    }
}